﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CCOMDataAccess;

/// <summary>
/// Summary description for UserManager
/// </summary>
public class UserManager
{
    #region const
    public UserManager()
    {
        //
        // TODO: Add constructor logic here
        //
    } 
    #endregion

    public static bool Authenticate(string login, string password)
    {
        DataClassesDataContext db = new DataClassesDataContext();
        var UserInfos = from u in db.Users
                        where u.Pseudo == login
                        select new
                        {
                            ID = u.Matricule,
                            UserPassword = u.Password,
                        };
        if (UserInfos.First().UserPassword == password)
        {
            HttpContext.Current.Session["UserID"] = UserInfos.First().ID;
            HttpContext.Current.Session["isLogged"] = true;
            return true;
        }
        else
        {
            return false;
        }
    }

    public static void Logout()
    {
        try
        {
            HttpContext.Current.Session["UserID"] = null;
            HttpContext.Current.Session["islogged"] = null;
            return;
        }
        catch (NullReferenceException)
        {
            return;
        }
    }

    public static User getUserInfos(int id)
    {
        DataClassesDataContext db = new DataClassesDataContext();
        var UserInfos = from u in db.Users
                        where u.Matricule == id
                        select u;
        return (User)UserInfos.First();
    }
}
